package ph.com.gs3.formalistics.presenter.adapters;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.global.constants.StarMark;
import ph.com.gs3.formalistics.global.utilities.DateUtilities;
import ph.com.gs3.formalistics.model.vo.view.DocumentDisplayItem;
import ph.com.gs3.formalistics.services.managers.ImageManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class DocumentListItemViewAdapter extends BaseAdapter {

	public static final String TAG = DocumentListItemViewAdapter.class.getSimpleName();

	private Context context;
	private List<DocumentDisplayItem> documentSummarylist;

	private DocumentListItemActionListener listener;

	private ImageManager imageManager;

	public DocumentListItemViewAdapter(Context context) {
		this.context = context;

		this.documentSummarylist = new ArrayList<>();
		this.documentSummarylist.addAll(documentSummarylist);

		imageManager = ImageManager.getDefaultInstance(context.getApplicationContext());

	}

	public void setViewData(List<DocumentDisplayItem> documentSummarylist) {
		this.documentSummarylist.clear();
		this.documentSummarylist.addAll(documentSummarylist);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return documentSummarylist.size();
	}

	@Override
	public Object getItem(int position) {
		return documentSummarylist.get(position);
	}

	@Override
	public long getItemId(int position) {
		return documentSummarylist.get(position).getDocumentId();
	}

	public static class DocumentListViewFragmentHolder {

		public TextView tvHeader;
		public TextView tvSubheader;
		public TextView tvDateOnHeader;
		public TextView tvContentsHeader;
		public TextView tvContentsBody;
		public TextView tvFooterTitle;
		public TextView tvFooterContent;

		public TextView tvBadge;

		public Button bShowComments;
		public Button bShowActions;
		public Button bMarkDocumentStar;

		public ImageView avatar;

		public DocumentListViewFragmentHolder(View view) {

			tvHeader = (TextView) view.findViewById(R.id.DL_tvheader);
			tvSubheader = (TextView) view.findViewById(R.id.DL_tvSubHeader);
			tvDateOnHeader = (TextView) view.findViewById(R.id.DL_tvheader_action_date);
			tvContentsHeader = (TextView) view.findViewById(R.id.DL_tvcontentsHeader);
			tvContentsBody = (TextView) view.findViewById(R.id.DL_tvcontents);
			tvFooterTitle = (TextView) view.findViewById(R.id.DL_tvfooter_title);
			tvFooterContent = (TextView) view.findViewById(R.id.DL_tvfooter_content);

			tvBadge = (TextView) view.findViewById(R.id.DL_tvbadge);

			bShowComments = (Button) view.findViewById(R.id.DL_bComment);
			bShowActions = (Button) view.findViewById(R.id.DL_bActions);
			bMarkDocumentStar = (Button) view.findViewById(R.id.DL_bStar);

			avatar = (ImageView) view.findViewById(R.id.DL_ivAvatar);

		}

	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		// Animation fadeInAnimation;
		// fadeInAnimation = AnimationUtils.loadAnimation(context, R.animator.fade_in);

		View row = convertView;
		DocumentListViewFragmentHolder holder = null;

		final DocumentDisplayItem documentSummary = documentSummarylist.get(position);

		if (row == null) {

			LayoutInflater inflater = (LayoutInflater) context
			        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.view_document_list_item, parent, false);
			holder = new DocumentListViewFragmentHolder(row);
			row.setTag(holder);

		} else {
			holder = (DocumentListViewFragmentHolder) row.getTag();
			// row.setAnimation(fadeInAnimation);
		}

		holder.bShowActions.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null) {
					listener.onClickedDocumentActions(documentSummary);
				}
			}
		});

		holder.bShowComments.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null) {
					listener.onClickedComments(documentSummary);
				}
			}
		});

		holder.bMarkDocumentStar.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				boolean isStarred = documentSummary.isStarred();

				if (isStarred) {
					listener.onDocumentStarMarked(documentSummary, StarMark.UNSTARRED);
				} else {
					listener.onDocumentStarMarked(documentSummary, StarMark.STARRED);
				}

				documentSummary.setStarred(!isStarred);

			}
		});

		String viewDate;
		try {
			Date dateUpdated = DateUtilities.parseToServerDate(documentSummary.getDate());
			viewDate = DateUtilities.WIDGET_DISPLAY_DATE_ONLY_FORMAT.format(dateUpdated);
		} catch (ParseException e) {
			viewDate = "Invalid Date";
		}

		holder.tvHeader.setText(documentSummary.getHeader());
		holder.tvSubheader.setText(documentSummary.getSubheader());
		holder.tvDateOnHeader.setText(viewDate);
		holder.tvContentsHeader.setText(documentSummary.getContentsHeader());
		holder.tvContentsBody.setText(documentSummary.getContentsBody());
		holder.tvFooterTitle.setText(documentSummary.getFooterHeader());
		holder.tvFooterContent.setText(documentSummary.getFooterBody());

		// Hide the body if it should be empty
		if (documentSummary.getContentsHeader() == null
		        || documentSummary.getContentsHeader().isEmpty()) {
			holder.tvContentsHeader.setVisibility(View.GONE);
		}

		if (documentSummary.getContentsBody() == null
		        || documentSummary.getContentsBody().isEmpty()) {
			holder.tvContentsBody.setVisibility(View.GONE);
		}

		if (documentSummary.isStarred()) {
			holder.bMarkDocumentStar.setBackgroundResource(R.drawable.starstrokeborderedpressed);
		} else {
			holder.bMarkDocumentStar.setBackgroundResource(R.drawable.starstrokebordered);
		}

		// Button visibility
		holder.bShowActions.setVisibility(documentSummary.isShowActionsButton() ? View.VISIBLE
		        : View.GONE);
		holder.bMarkDocumentStar.setVisibility(documentSummary.isShowStarButton() ? View.VISIBLE
		        : View.GONE);
		holder.bShowComments.setVisibility(documentSummary.isShowCommentButton() ? View.VISIBLE
		        : View.GONE);

		imageManager.requestUserImage(documentSummary.getDocumentAuthorId(), holder.avatar);

		// Default update type/current
		holder.tvBadge.setVisibility(View.GONE);

		// if (userActionListViewItem.updateType == UpdateType.NEW) {
		// holder.tvBadge.setText("New");
		// holder.tvBadge.setAnimation(fadeInAnimation);
		// // row.setAnimation(fadeInAnimation);
		//
		// holder.tvBadge.setVisibility(View.VISIBLE);
		// } else if (userActionListViewItem.updateType == UpdateType.UPDATE) {
		// holder.tvBadge.setText("Updated");
		// holder.tvBadge.setAnimation(fadeInAnimation);
		// // row.setAnimation(fadeInAnimation);
		//
		// holder.tvBadge.setVisibility(View.VISIBLE);
		// }

		return row;
	}

	public void setActionListener(DocumentListItemActionListener listener) {
		this.listener = listener;
	}

	public static interface DocumentListItemActionListener {

		public void onClickedDocumentActions(DocumentDisplayItem documentDisplayItem);

		public void onDocumentStarMarked(DocumentDisplayItem documentDisplayItem,
		        StarMark newDocumentStar);

		public void onClickedComments(DocumentDisplayItem documentDisplayItem);

	}

}
